// pages/order_pay/order_pay.js
const app = getApp();
var request = require("../../utils/request.js");
var wx_alert = require("../../utils/wx_alert.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    read:true,
    order_lists:[   
    ],
    show_have: true,
    total_price: "",
    phone_state: true        
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //快捷导航跳转
    this.turn = request.turn;
    this.close_modal = request.close_modal;
    if (wx.getStorageSync("totalInfo").mobile || wx.getStorageSync("mobile")){
      this.setData({
        phone_state:false
      })
    }

    var send_order = wx.getStorageSync("send_order");
    var total_price = 0;
    var upgradeIds = [];
    var snId;
    for (var i in send_order){
      send_order[i]["num"] = 1;
      total_price += Number(send_order[i].num * send_order[i].price);

      if (send_order[i].selected == true){
        upgradeIds.push(send_order[i].id)
      }else{
        snId = send_order[i].snId
      }

    }
    this.setData({
      order_lists: send_order,
      total_price: total_price
    })
    

    var order_data = {
      storeId: options.storeId,
      productId: wx.getStorageSync("productId"),
      snId: snId,
      upgradeIds: upgradeIds,
      shootTime: options.sent_date
    }
    console.log(order_data)
    request.request(app.globalData.addOrder, order_data, function(res){
      console.log(res)
      if(res.code == 0){
        that.setData({
          orderEx: res.orderEx,
          img_url: request.url + "/",
          shootTime: options.sent_date,
          order_id: res.orderEx.id
        })
      }
    })
    
    var getStore_data = {
      limit:2,
      page:1,
      city:"南京",
      keyword:"",
      classifyId:"0",
      sort:"1",
      brandId:"0"
    }
    request.request(app.globalData.getStoreList, getStore_data,function(res){
      console.log(res)
      if(res.code ==0){
        for (var j in res.storeExlist){
          var label = res.storeExlist[j].label;
          res.storeExlist[j].label = label.split(",");          
        }
        that.setData({
           order_storelist: res.storeExlist
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  now_pay:function(){
    
    request.request(app.globalData.payForOrder, { orderId: this.data.order_id, mobileType: "phone", payType:"26-3"},function(res){
        console.log(res)
        if(res.code == 0){

          // wx.request({
          //   url: "https://www.99bill.com/mobilegateway/recvMerchantInfoAction.htm",
          //   data: res.params,
          //   header: {
          //     'content-type': 'application/xml'
          //   },
          //   method: "GET",
          //   success: function (res) {
          //     console.log(res)
             
          //   }
          // })
          wx.navigateTo({
            url: '/pages/web-view/web?orderId=' + res.params.orderId,
          })
          
        }
    })


    // wx.navigateTo({
    //   url: '/pages/mine_center/payment/index_2',
    // })
  },
  have_read:function(){
    var read = this.data.read;
    read = !read;
    this.setData({
      read: read
    })
  },
  have_reads:function(){
    wx.navigateTo({
      url: '/pages/service_agreement/agreement',
    })    
  },
  // 加
  plus:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_lists = this.data.order_lists;

    order_lists[index].num++;
    var data;
    for (var i in order_lists){
      if (order_lists[index].snId){
         data = {
           orderId: this.data.order_id,
           productId: wx.getStorageSync("productId"),
           goodNum: order_lists[index].num,           
         }
      }else{
        data = {
          orderId: this.data.order_id,
          productId: wx.getStorageSync("productId"),
          upNum: order_lists[index].num,
          upId: order_lists[index].id
        }        
      }
    }

    request.request(app.globalData.changeOrderNum,data,function(res){
      console.log(res)
      if(res.code == 0){
        that.setData({
          order_lists: order_lists
        })
        num(index, that, "plus");
      }
    })

  },
  // 减
  pluase:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_lists = this.data.order_lists;    
    if (order_lists[index].num == 1){
      wx_alert.confirm("确定删除此商品吗？",function(){
        num(index, that, "pluase");             
        order_lists.splice(index,1);
        that.setData({
          order_lists: order_lists
        })  
      })
    }else{
      order_lists[index].num--;
      
      var data;
      for (var i in order_lists) {
        if (order_lists[index].snId) {
          data = {
            orderId: this.data.order_id,
            productId: wx.getStorageSync("productId"),
            goodNum: order_lists[index].num,
          }
        } else {
          data = {
            orderId: this.data.order_id,
            productId: wx.getStorageSync("productId"),
            upNum: order_lists[index].num,
            upId: order_lists[index].id
          }
        }
      }
      request.request(app.globalData.changeOrderNum, data, function (res) {
        console.log(res)
        if (res.code == 0) {
          console.log("成功")
          that.setData({
            order_lists: order_lists
          })
          num(index, that, "pluase");
        }
      })      

    } 
  },
  call_phone: function (e) {
    console.log(e)
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },  
  getPhoneNumber: function (e) {

    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      var data = {
        sessionId: wx.getStorageSync('sessionId'),
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
      request.request(app.globalData.proBindMobile, data, function (res) {
        console.log(res)
        if (res.code == 0) {
          that.setData({
            phone_state: false,
            "user.mobile": res.phone
          })
        } else {
          wx_alert.alert("授权失败，请先登录", function (res) {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          })
        }
      })

    } else {
      wx_alert.msg("您拒绝了小程序的授权");
      this.setData({
        phone_state: false
      })
    }

  }, 
})
function num(index, that, status){
  var order_lists = that.data.order_lists;
  var total_price = Number(that.data.total_price);
  var price = order_lists[index].price;
  if (status =="plus"){
    total_price += Number(price);
  }else{
    total_price -= Number(price);
  }

  that.setData({
    total_price: total_price
  })
}